using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Geekees.App.Cashbook.Lib;
using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Entity;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Cashbook.
	/// </summary>
	public class Cashbook : PageBase
	{
		protected System.Web.UI.WebControls.TextBox txtAmount;
		protected System.Web.UI.WebControls.DropDownList ddlCategory;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.RadioButtonList rblOutcomeIncome;
		protected System.Web.UI.WebControls.TextBox txtTag;
		protected System.Web.UI.WebControls.Literal lUserName;
		protected System.Web.UI.WebControls.TextBox txtDetail;
		protected System.Web.UI.WebControls.TextBox txtDate;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
				InitialPage();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void InitialPage()
		{
			this.lUserName.Text = LoginManager.GetCurrentUser().Name;
			this.txtDate.Text =  LoginManager.GetCurrentUser().TimeZone.Now.ToString( "yyyy-MM-dd HH:mm" );
			BindData();
		}

		private void BindData()
		{
			Binder.BindCategory( this.ddlCategory, ListPromptType.Select );
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if( DoSave() )
				FlashMessage.SetFlash( "Save Successful!" );
		}

		private bool DoSave()
		{
			float amount = -1;
			DateTime date = Consts.MIN_DATE;
			int categoryId = -1;
			string detail = string.Empty;
			
			#region get amount
			try
			{
				amount = float.Parse( this.txtAmount.Text.Trim() );
			}
			catch{}
			if( amount < 0 )
			{
				FlashMessage.SetFlash( "Please give a number for amount!", FlashMessage.FlashMessageType.Error );
				return false;
			}
			#endregion

			#region get category
			if( this.ddlCategory.SelectedIndex <= 0 )
			{
				FlashMessage.SetFlash( "Please select a category!", FlashMessage.FlashMessageType.Error );
				return false;
			}
			categoryId = int.Parse(this.ddlCategory.SelectedValue);
			#endregion

			#region get detail
			detail = this.txtDetail.Text.Trim();
			if( detail == string.Empty )
			{
				FlashMessage.SetFlash( "Please write some detail!", FlashMessage.FlashMessageType.Error );
				return false;
			}
			#endregion

			#region get date
			try
			{
				date = DateTime.Parse( this.txtDate.Text.Trim() ).ToUniversalTime();
			}
			catch
			{}

			if( date == Consts.MIN_DATE )
			{
				FlashMessage.SetFlash( "Invalid Date!", FlashMessage.FlashMessageType.Error );
				return false;
			}
			#endregion

			#region get type
			CashItemType type = CashItemType.Outcome;
			switch( this.rblOutcomeIncome.SelectedValue )
			{
				case "1":
					type = CashItemType.Outcome;
					break;
				case "2":
					type = CashItemType.Income;
					break;
			}
			#endregion

			int itemId = BLCashItem.AddCashItem( amount, detail, categoryId, date , this.txtTag.Text.Trim(), type);

			return itemId > 0;
		}

	}
}
